package com.xgkj.diyiketang.activity.data;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.baidu.mapapi.search.core.CityInfo;
import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.data.AreaInfo;
import com.xgkj.diyiketang.data.MessageInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrater on 2015/12/28.
 */
public class CityAdapter extends BaseAdapter {
    private Context mContext;
    private List<AreaInfo> mList = new ArrayList<AreaInfo>();

    public CityAdapter(Context context) {
        mContext = context;
    }

    public void updateData(List<AreaInfo> list) {
        if (mList != null) {
            mList.addAll(list);
        }
        notifyDataSetChanged();
    }

    public void clearData() {
        if (mList != null) {
            mList.clear();
        }
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (mList != null) {
            return mList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (mList != null) {
            return mList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ChildHolder childHolder = null;
        if (convertView == null || convertView.getTag() == null) {
            childHolder = new ChildHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.city_item, null);
            childHolder.cityName = (TextView) convertView.findViewById(R.id.city_name);
            convertView.setTag(childHolder);
        } else {
            childHolder = (ChildHolder) convertView.getTag();
        }

        AreaInfo cinfo = mList.get(position);
        if (cinfo != null) {
            childHolder.cityName.setText(cinfo.getAreaName());

        }

        return convertView;
    }

    class ChildHolder {
        TextView cityName;
    }
}